//
//  ProductDetailViewController.m
//  DaDaLiCai
//
//  Created by User on 15/11/11.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "ProductDetailViewController.h"
#import "ProductDetailCell.h"
#import "LargeInvestmentCell.h"
#import "PayDetailViewController.h"

@interface ProductDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) ProductModel *model;

@property (nonatomic, strong) UITableView *listTableView;

@property (nonatomic, strong) UIButton *investBt;

@end

@implementation ProductDetailViewController

- (id)initWithModel:(ProductModel *)model
{
    self = [super init];
    
    if(self)
    {
        _model = model;
    }
    return self;
}

//创建导航栏
-(void)createTheNavigationBar
{
    TopView *topView = [[TopView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_HEIGHT, 64.0f)];
    topView.backgroundColor = [UIColor hexStringToColor:COLOR_VALUE];
    [topView setTitile:@"产品详情"];
    [self.view addSubview:topView];
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 35.0f, 49.0f, 16.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
}

- (UIButton *)investBt
{
    if(!_investBt)
    {
        _investBt = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, SCREEN_HEIGHT - 50.0f, SCREEN_WIDTH, 50.0f)];
        [_investBt setBackgroundColor:[UIColor hexStringToColor:COLOR_VALUE] forState:UIControlStateNormal];
        _investBt.titleLabel.font = [UIFont systemFontOfSize:16];
        [_investBt setTitle:@"立即投资" forState:UIControlStateNormal];
        [_investBt addTarget:self action:@selector(buttonInvestTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_investBt setTitleColor:[UIColor hexStringToColor:@"#FFFFFF"] forState:UIControlStateNormal];
    }
    return _investBt;
}
- (UITableView *)listTableView
{
    if(!_listTableView)
    {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT - 114.0f)];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    }
    return _listTableView;
}

- (void)buttonBackTapped:(id)sender
{
    [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:TRUE];
}

- (void)buttonInvestTapped:(id)sender
{
    [[AnimationIndicator sharedIndicator]startAnimation];
    [AppGlobalClass PostDataWithNSstring:CHECKCHARGE withDictionary:nil success:^(NSDictionary *dictionary) {
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            PayDetailViewController *payDetailViewCon = [[PayDetailViewController alloc]initWithModel:_model];
            [self.navigationController pushViewController:payDetailViewCon animated:TRUE];
        }else
        {
            ALERT_VIEW(@"该产品暂时无法购买");
        }
        [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:TRUE];
    } failure:^(NSError *error) {
        [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:TRUE];
    } networkerror:^(NSString *networkerror) {
        [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:TRUE];
    }];
    
    
}

#pragma mark-----------dataSource和delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
        return 4;
    else
        return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 10.0f)];
    footerView.backgroundColor = [UIColor hexStringToColor:@"#e7e7e7"];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 0)
        return 10.0f;
    else
        return 0.0f;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        ProductDetailCell *cell = [[ProductDetailCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell0"];
        if(indexPath.row == 0)
            cell.textLabel.text = _model.pname;
        cell.textLabel.numberOfLines = 0;
        if(indexPath.row == 1)
        {
            cell.textLabel.text = @"编码";
            cell.detailTextLabel.text = _model.pcode;
        }
        if(indexPath.row == 2)
        {
            cell.textLabel.text = @"产品期限";
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@天",_model.revperiod];
        }
        if(indexPath.row == 3)
        {
            cell.textLabel.text = @"预期收益";
            
            float revate = ([_model.revrate intValue])/10.0;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f%%",revate];
        }
        
        return cell;
    }
    
    if(indexPath.section == 1)
    {
        LargeInvestmentCell *cell = [[LargeInvestmentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1" andIndexPath:indexPath];
        
        if(indexPath.row == 1)
        {
            cell.textLabel.text = _model.paname;
        }
        
        return cell;
    }
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        if ([UILabel height:_model.pname widthOfFatherView:SCREEN_WIDTH - 15.0f textFont:[UIFont systemFontOfSize:16.0f]] <50)
        {
            return 50;
        }
        return [UILabel height:_model.pname widthOfFatherView:SCREEN_WIDTH - 15.0f textFont:[UIFont systemFontOfSize:16.0f]];
                                                                                                                                                                                                                                                        
    }
    else
        return 50.0f;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTheNavigationBar];
    [self.view addSubview:self.listTableView];
    [self.view addSubview:self.investBt];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
